Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_playerRoot
    Inherits Csla.BusinessBase(Of rw_playerRoot)

#Region " Business Properties and Methods "

    ' declare members
    Private _playerId As Integer = 0
    Private _teamId As Integer = 0
    Private _playernameFirst As String = String.Empty
    Private _playernameLast As String = String.Empty

    ''' <summary>
    ''' Get the PlayerId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the PlayerId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, True)> _
    Public ReadOnly Property PlayerId() As Integer
        Get
            CanReadProperty("PlayerId", True)
            Return _playerId
        End Get
    End Property

    ''' <summary>
    ''' Get or Set the TeamId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the TeamId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property TeamId() As Integer
        Get
            CanReadProperty("TeamId", True)
            Return _teamId
        End Get
        Set(ByVal value As Integer)
            CanWriteProperty("TeamId", True)
            If Not _teamId.Equals(value) Then
                _teamId = value
                PropertyHasChanged("TeamId")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the PlayernameFirst of the current object.
    ''' </summary>
    ''' <value>A String that represents the PlayernameFirst of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property PlayernameFirst() As String
        Get
            CanReadProperty("PlayernameFirst", True)
            Return _playernameFirst
        End Get
        Set(ByVal value As String)
            CanWriteProperty("PlayernameFirst", True)
            If value Is Nothing Then value = String.Empty
            If Not _playernameFirst.Equals(value) Then
                _playernameFirst = value
                PropertyHasChanged("PlayernameFirst")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the PlayernameLast of the current object.
    ''' </summary>
    ''' <value>A String that represents the PlayernameLast of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property PlayernameLast() As String
        Get
            CanReadProperty("PlayernameLast", True)
            Return _playernameLast
        End Get
        Set(ByVal value As String)
            CanWriteProperty("PlayernameLast", True)
            If value Is Nothing Then value = String.Empty
            If Not _playernameLast.Equals(value) Then
                _playernameLast = value
                PropertyHasChanged("PlayernameLast")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _playerId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' PlayernameFirst rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "PlayernameFirst")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("PlayernameFirst", 50))
        ' PlayernameLast rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "PlayernameLast")
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("PlayernameLast", 50))
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in rw_playerRoot
        'AuthorizationRules.AllowRead("PlayerId", "rw_playerRootReadGroup")
        'AuthorizationRules.AllowRead("TeamId", "rw_playerRootReadGroup")
        'AuthorizationRules.AllowRead("PlayernameFirst", "rw_playerRootReadGroup")
        'AuthorizationRules.AllowRead("PlayernameLast", "rw_playerRootReadGroup")

        'AuthorizationRules.AllowWrite("TeamId", "rw_playerRootWriteGroup")
        'AuthorizationRules.AllowWrite("PlayernameFirst", "rw_playerRootWriteGroup")
        'AuthorizationRules.AllowWrite("PlayernameLast", "rw_playerRootWriteGroup")
    End Sub

    Public Shared Function CanGetObject() As Boolean
        'TODO: Define CanGetObject permission in rw_playerRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_playerRootViewGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanAddObject() As Boolean
        'TODO: Define CanAddObject permission in rw_playerRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_playerRootAddGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanEditObject() As Boolean
        'TODO: Define CanEditObject permission in rw_playerRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_playerRootEditGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanDeleteObject() As Boolean
        'TODO: Define CanDeleteObject permission in rw_playerRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("rw_playerRootDeleteGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

    Private Sub New()
        ' require use of factory method 
    End Sub

    Public Shared Function NewPlayerRoot() As rw_playerRoot
        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_playerRoot")
        End If
        Return DataPortal.Create(Of rw_playerRoot)()
    End Function

    Public Shared Function GetPlayerRoot(ByVal playerId As Integer) As rw_playerRoot
        If Not CanGetObject() Then
            Throw New System.Security.SecurityException("User not authorized to view a rw_playerRoot")
        End If
        Return DataPortal.Fetch(Of rw_playerRoot)(New Criteria(playerId))
    End Function

    Public Shared Sub DeleteplayerRoot(ByVal playerId As Integer)
        If Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_playerRoot")
        End If
        DataPortal.Delete(New Criteria(playerId))
    End Sub

    Public Overrides Function Save() As rw_playerRoot
        If IsDeleted AndAlso Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a rw_playerRoot")
        ElseIf IsNew AndAlso Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a rw_playerRoot")
        ElseIf Not CanEditObject() Then
            Throw New System.Security.SecurityException("User not authorized to update a rw_playerRoot")
        End If
        Return MyBase.Save()
    End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Criteria "

    <Serializable()> _
    Private Class Criteria

        Public PlayerId As Integer

        Public Sub New(ByVal playerId As Integer)
            Me.PlayerId = playerId
        End Sub
    End Class

#End Region 'Criteria

#Region " Data Access - Create "

    <RunLocal()> _
    Protected Overloads Sub DataPortal_Create(ByVal criteria As Object)
        ValidationRules.CheckRules()
    End Sub

#End Region ' Data Access - Create

#Region " Data Access - Fetch "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteFetch(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerroot_select"
            cm.Parameters.AddWithValue("@playerId", criteria.PlayerId)

            Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                FetchObject(dr)
                ValidationRules.CheckRules()

                'load child object(s)
                FetchChildren(dr)
            End Using
        End Using
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        dr.Read()
        _playerId = dr.GetInt32("playerId")
        _teamId = dr.GetInt32("teamId")
        _playernameFirst = dr.GetString("playerName_first")
        _playernameLast = dr.GetString("playerName_last")
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Protected Overrides Sub DataPortal_Insert()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteInsert(cn)

            'update child object(s)
            UpdateChildren(cn)
        End Using
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerroot_insert"

            AddInsertParameters(cm)

            cm.ExecuteNonQuery()

            _playerId = CType(cm.Parameters("@new_playerId").Value, Integer)
        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@teamId", _teamId)
        cm.Parameters.AddWithValue("@playerName_first", _playernameFirst)
        cm.Parameters.AddWithValue("@playerName_last", _playernameLast)
        cm.Parameters.AddWithValue("@new_playerId", _playerId)
        cm.Parameters("@new_playerId").Direction = ParameterDirection.Output
    End Sub

#End Region 'Data Access - Insert

#Region " Data Access - Update "

    Protected Overrides Sub DataPortal_Update()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            If MyBase.IsDirty Then
                ExecuteUpdate(cn)
            End If

            ' update child object(s)
            UpdateChildren(cn)
        End Using

    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerroot_update"

            AddUpdateParameters(cm)

            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@playerId", _playerId)
        cm.Parameters.AddWithValue("@teamId", _teamId)
        cm.Parameters.AddWithValue("@playerName_first", _playernameFirst)
        cm.Parameters.AddWithValue("@playerName_last", _playernameLast)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
    End Sub

#End Region 'Data Access - Update

#Region " Data Access - Delete "

    Protected Overrides Sub DataPortal_DeleteSelf()
        DataPortal_Delete(New Criteria(_playerId))
    End Sub

    Private Overloads Sub DataPortal_Delete(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteDelete(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_rw_playerroot_delete"

            cm.Parameters.AddWithValue("@playerId", criteria.PlayerId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region 'Data Access


End Class

